package com.mycompany.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.mycompany.client.model.CalendarBO;
import com.mycompany.server.config.JDOPersistenceManagerFactory;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl  {

	public CalendarBO addCalendar(CalendarBO calendarBO) {
		try {
			PersistenceManager pm = JDOPersistenceManagerFactory.get().getPersistenceManager();
			calendarBO = pm.makePersistent(calendarBO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendarBO;
	}

	public CalendarBO updateCalendar(CalendarBO calendarBO) {
		try {
			PersistenceManager pm = JDOPersistenceManagerFactory.get().getPersistenceManager();
			calendarBO = pm.makePersistent(calendarBO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendarBO;
	}

	public Boolean deleteCalendar(CalendarBO calendarBO) {
		try {
			PersistenceManager pm = JDOPersistenceManagerFactory.get().getPersistenceManager();
			pm.deletePersistent(calendarBO);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public List<CalendarBO> loadCalendars(String userID) {
		List<CalendarBO> calendars = new ArrayList<CalendarBO>();
		try {
			PersistenceManager pm = JDOPersistenceManagerFactory.get().getPersistenceManager();
			Query query = pm.newQuery(CalendarBO.class);
			List<CalendarBO> rs = (List<CalendarBO>)query.execute();
			for(CalendarBO c : rs) {
				calendars.add(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendars;
	}
}
